const db=wx.cloud.database()
const shop=db.collection('shop')
let app=getApp()
Page({
    data:{
        list:[],
        skip:20,
        isShow:false
    },
    async load(){
        wx.showLoading({
            title: '正在加载中',
            mask:true
          })
          let res
          try{
              res=await wx.getStorage({//从缓存获取登陆信息并写入内存
                  key:'user'
               })
               console.log(res)
               if(res.data.avatarUrl){
                   app.globalData.avatarUrl=res.data.avatarUrl
                   app.globalData.nickName=res.data.nickName
                   app.globalData.openid=res.data.openid
                   console.log(app.globalData.nickName)
                   console.log(app.globalData.openid)
               }
          }catch(e){
              console.log(e)
          }
          //加载店铺信息
          res=await db.collection('shop').where({
                        isGoodShop:true
                      })
                      .get()
          console.log('goodShop',res)
          console.log(res.data[0].cai.length)
          let gList=res.data
          this.setData({gList})
          res=await shop.where({
                          isGoodShop:false
                      })
                      .get()
          let list=res.data
          this.setData({
              list,
              isShow:true
            })
          wx.hideLoading()
    },
    async onLoad(){
       this.load()
    },
    //跳转页面↓↓↓
    gJumpPage(e){
        console.log('jumpPage',e)
        wx.navigateTo({
          url: '/pages/orther/content?id='+e.currentTarget.dataset.id,
        })
    },
    aJumpPage(e){
        console.log('jumpPage',e)
        wx.navigateTo({
          url: '/pages/orther/content?id='+e.currentTarget.dataset.id,
        })
    },
    async onReachBottom(){
        let {list,skip}=this.data
        let res=await shop.where({
            isGoodShop:false
        }).skip(skip).limit(20)
        .get()
        skip+=20
        for(let i=0;i<20;i++){
            if(!res.data[i]){
                break
            }
            list.push(res.data[i])
        }
        this.setData({
            list,skip
        })
    },
    onPullDownRefresh(){
        let that=this
        that.setData({
            isShow:false
        })
        wx.stopPullDownRefresh({
          success(){
              that.load()
          }
        })
    }
})